<?php
/**
 * 调用公众号h5支付所需要的支付接口方法
 * actionNativeNotify()为扫码支付统一下单回调地址
 * @author ran.ran
 * date 2016-4-18
 */
namespace api\controllers;

use Yii;
use services\models\NativeNotifyCallBack;
use yii\web\NotFoundHttpException;
use common\helper\UrlManager;

class PaymentController extends BaseCoreController
{
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'wxpay'=>[
                'class'=>'components\JsapiPayAction',
                'successUrl'=>UrlManager::createUrl(Yii::$app->params['domains']['wap_url'], ['order/payed','order_info_id'=>'']),
                'failUrl'=>UrlManager::createUrl(Yii::$app->params['domains']['wap_url'],['order-details/listwap']),
            ]
        ];
    }
    
    /**
     * 扫码支付统一下单回调
     */
    public function actionNativeNotify() {
        try {
            
            $xml = $GLOBALS["HTTP_RAW_POST_DATA"];
            
            $postObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
            //验证数据合法性
            $appid = Yii::$app->params['wxpay']['appid'];
            $mchid = Yii::$app->params['wxpay']['mchid'];
            
            if($postObj->appid != $appid ||$postObj->mch_id != $mchid ) {
                throw new NotFoundHttpException('sign error');
            }
            
            $notify = new NativeNotifyCallBack();
            $notify->Handle(true);
            
        } catch (\Exception $e) { 
            throw new NotFoundHttpException('sign error');
        }
    }
}

?>